library(pacman)
pacman::p_load(tidyverse,data.table,broom,readxl,writexl,openxlsx)
rm(list=ls())
###############################################

####SCC level
SCC_nominal = 50

####Spatial units
df_u = read.xlsx('aux_params/parameters.xlsx', sheet='spatial_units')
N_spatial_units = nrow(df_u)
df_u_stacked = rbind(df_u, df_u)

####Price level - model
n=1
for(model_version in c('baseline','mobility') ){
  
  if(model_version=='baseline'){conduct_list=c('ic','pc')}else{conduct_list=c('ic')}
  
  for(conduct in conduct_list){

    if(model_version=='baseline'){folder=paste0('results_',conduct,'/lf')}else{folder=paste0('results_',conduct,'/lf/mobility')}

    df = fread(paste0(folder,'/p_ij_c.csv'))
    colnames(df) <- c('SA','EU','AS','ROW')
    df$p_ij = (df$SA+df$EU+df$AS+df$ROW)/4
    df$spatial_id_name = df_u_stacked$spatial_id_name
    df$commodity='beef'
    df[(N_spatial_units+1):(2*N_spatial_units),]$commodity = 'crops'
    df = df[commodity=='beef',list(spatial_id_name,p_ij)]
    df=df[,-c('spatial_id_name')][, lapply(.SD, mean, na.rm=TRUE)]
    df$conduct = conduct
    df$model_version = model_version
    
    if(n==1){df_summary = df}else{df_summary = rbind(df_summary,df)}
    n=n+1
    
  }
}

#Price level - data
p_ij_data = 4000

#Relative price level
df = df_summary
df$p_ij_scale = df$p_ij/p_ij_data

#Adjust nominal SCC to model-consistent SCC
df$SCC = df$p_ij_scale*SCC_nominal
df$SCC_value = SCC_nominal
df_final = df[,list(model_version, conduct, SCC, SCC_value)]
list_of_datasets <- list("baseline_ic" = df_final[model_version=='baseline' & conduct=="ic",],
                         "baseline_pc" = df_final[model_version=='baseline' & conduct=="pc",],
                         "mobility_ic" = df_final[model_version=='mobility' & conduct=="ic",])
write_xlsx(list_of_datasets, path = "aux_params/scc.xlsx")


